package com.alib.springcloud.controller;

import com.alib.springcloud.entities.CommonResult;
import com.alib.springcloud.entities.Payment;
import com.alib.springcloud.service.PaymentService;
import io.micrometer.core.instrument.util.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {

    @Autowired
    PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource // 实质上是 Fegin 的服务发现
    DiscoveryClient discoveryClient;

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("*****查询结果：" + payment);

        if( payment != null){
            return new CommonResult(200,"查询成功！服务端口："+serverPort,payment);
        }else{
            return new CommonResult(400,"查询失败！查询ID：" + id,null);
        }
    }

    @PostMapping("/payment/create")
    public CommonResult<Payment> insertPayment(@RequestBody Payment payment){
        int insertResult = paymentService.create(payment);
        log.info("*****插入结果：" + insertResult);

        if( insertResult ==1 ){
            return new CommonResult(200,"插入成功！服务端口："+serverPort, insertResult);
        }else{
            return new CommonResult(400,"插入失败！插入数据：" + payment.toString(),null);
        }
    }

    @GetMapping( value = "/payment/discovery")
    public Object getDiscovery(){

        // 发现全部服务
        List<String> services = discoveryClient.getServices();
        log.info("**** services ***");
        for (int i = 0; i < services.size(); i++) {
            log.info(services.get(i));
        }

        // 发现某个服务下的全部集群
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-PAYMENT");
        log.info("*** instances ***");
        for(ServiceInstance instance : instances){
            log.info(instance.getServiceId());
            log.info(instance.getHost());
            log.info(instance.getUri().toString());
            log.info(String.valueOf(instance.getPort()));
            log.info(instance.getMetadata().toString());
            log.info(instance.getInstanceId());
            log.info(instance.getScheme());
            log.info(instance.getClass().toString());
        }

        return this.discoveryClient;
    }

    @GetMapping(value = "/payment/lb")
    public String getLbPayment(){
        return serverPort;
    }

    @GetMapping(value = "/payment/timeout")
    public String getPaymentTimeout(){
//        try {
//            TimeUnit.SECONDS.sleep(3);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return serverPort;
    }


    // 服务链路监控 测试 ziplin + sleuth
    @GetMapping("/payment/zipkin")
    public String paymentZipkin()
    {
        return "提供者--->" + serverPort + "zipkin ---->";
    }

}
